package cn.ihrm.sass.controller;

import cn.common.Result;
import cn.common.ResultCode;
import cn.controller.BaseController;
import cn.exception.CommonException;
import cn.ihrm.sass.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

//解决跨域问题
@CrossOrigin

@Api(description = "权限管理",value = "PermissionController")
@RestController
@RequestMapping(value = "/permission")
public class PermissionController extends BaseController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 保存权限
     * @param map
     * @return
     */
    @ApiOperation(value = "保存权限")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public Result save(@RequestBody Map<String,Object> map) throws Exception {
        // 设置保存的企业id
        //permission.setCompanyId(companyId);
        //permission.setCompanyName(companyName);

        //业务操作
        permissionService.add(map);
        //return new Result(ResultCode.SUCCESS);
        return new Result(10000,"操作成功！",true);
    }

    /**
     * 根据id更新权限
     * 1.方法
     * 2.请求参数
     * 3.响应
     */
    @ApiOperation(value = "根据id更新权限")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(@PathVariable(value = "id") String id, @RequestBody Map<String,Object> map) throws Exception {

        map.put("id",id);
        permissionService.update(map);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id删除权限
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除权限")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable(value = "id") String id) throws CommonException {
        permissionService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id查询权限
     * @param id
     * @return
     * @throws CommonException
     */
    @ApiOperation(value = "根据id查询权限")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable(value = "id") String id) throws CommonException {

        Map<String,Object> map = permissionService.findById(id);
        Result result = new Result(ResultCode.SUCCESS,map);
        return result;
    }

    /**
     * 查询全部权限列表
     * @return
     */
    @ApiOperation(value = "查询全部权限列表")
    @RequestMapping(value = "findAll", method = RequestMethod.GET)
    public Result findAll(@RequestBody Map<String,Object> map) {
        permissionService.findAll(map);

        Result result = new Result(ResultCode.SUCCESS);
        return result;
    }
}
